var ctx=window.daijia.ctx;
var year,months,companyId;
var xdData = new Array(12);
var cjData = new Array(12);
var jeData = new Array(12);
var djmonth="";
var djday="";
var djhour="";
$(function(){
	$("#export_button").click(function(){
		window.location=ctx+"/chart/export?exportYear="+$("#year").val()+(isMain?"&exportCompanyId="+$("#companyId").val():"")+
		"&exportMonth="+djmonth+"&exportDay="+djday;;
	});
	$("#search_button").click(function(){
		djmonth="";
		djday="";
		djhour="";
		var y = $("#year").val();
		var c=$("#companyId").val();
		year = $("#year").val();
		companyId = $("#companyId").val();  
		loadData();
		var cName=$('#companyId').find("option:selected").text();
		if($('.djloadblacklayer').length==0){
			var dw = $("#daijia").width();
			var dh = $("#daijia").height();
			var backlayer ="<div class='djloadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
			$("#daijia").append(backlayer);
			$('.djloadblacklayer').css({"z-index":"12","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
		}
		$.post(window.daijia.ctx+'/chart/dataForYear',{'year':y,'companyId':c},function(data){
			$('.djloadblacklayer').remove();
			
			months = data.months;
			
			if(months){
				for(var i=0 ; i<months.length ; i++){
					var month = months[i];
					xdData[month.month] = month.totalCancelNum;
					cjData[month.month] = month.totalSuccessNum;
					jeData[month.month] = month.totalPrice;
				}
				   
				renderYearChart(xdData,cjData,jeData,months);
			}else{
				$.scojs_message('公司 '+cName+' 没有订单记录', $.scojs_message.TYPE_ERROR);
				for(var i=0 ; i<12 ; i++){
					xdData[i] = 0;
					cjData[i] = 0;
					jeData[i] = 0;
				}
				renderYearChart(xdData,cjData,jeData,months);
			}
			
		},'json');
		
	});
	
	for(var i=0 ; i<12 ; i++){
		xdData[i] = 0;
		cjData[i] = 0;
		jeData[i] = 0;
	}
	
	Highcharts.setOptions({
		lang:{
			printChart:'打印报表',
			downloadJPEG:'保存为JPG图片',
			downloadPDF: '保存为PDF文档',
			downloadPNG: '保存为PNG图片',
			downloadSVG: '保存为SVG文档',
			loading:'加载中...',
			resetZoom:'重置缩放',
			resetZoomTitle:'缩放比例：1:1',
			months:['1月', '2月', '3月', '4月', '5月', '6月','7月', '8月', '9月', '10月', '11月', '12月']
		}
	});
	
	year = $("#year").val();
	companyId = $("#companyId").val();
	
	
	if($('.djloadblacklayer').length==0){
		var dw = $("#daijia").width();
		var dh = $("#daijia").height();
		var backlayer ="<div class='djloadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$("#daijia").append(backlayer);
		$('.djloadblacklayer').css({"z-index":"12","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
	
	$.post(window.daijia.ctx+'/chart/dataForYear',{'year':year,'companyId':companyId},function(data){
		$('.djloadblacklayer').remove();
		months = data.months;
		
		if(months){
			for(var i=0 ; i<months.length ; i++){
				var month = months[i];
				xdData[month.month] = month.totalCancelNum;
				cjData[month.month] = month.totalSuccessNum;
				jeData[month.month] = month.totalPrice;
			}
			
			renderYearChart(xdData,cjData,jeData,months);
		}else{
			$.scojs_message('没有订单记录', $.scojs_message.TYPE_OK);
		}
		
	},'json');
	
	/*$("#year").change(function(){
		$("#submit_form").submit();
	});
	
	$("#companyId").change(function(){
		$("#submit_form").submit();
	});*/
	
});

renderYearChart=function(xdData,cjData,jeData,months){

	$("#chart").highcharts({
		chart: {
            renderTo: 'chart',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		var category = e.point.category;
	            		//加载月数据 
	            		
	            		var year = $("#year").val();
	            		var month = parseInt(category.substring(0,category.length-1))-1;
	            		djmonth=month;
	            		
	            		var days = new Array();
	            		var xdmData = new Array();
	            		var cjmData = new Array();
	            		var jemData = new Array();
	            		
            			var currentMonth = months[month];
            			var days = currentMonth.chartDays;
            			var day = new Array();
            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\天</th>";
            			var tr1='<td>销单量</td>';
            			var tr2='<td>完成单量</td>';
            			var tr3='<td>金额(元)</td>';
        	        	$("#daijiaHead tr").remove();
        	        	$("#daijiaHead").append(head);
            			for(var i = 0 ; i < days.length ; i++){
            				var chartDay = days[i];
            				day[i] = chartDay.day;
            				xdmData[i] = chartDay.totalCancelNum;
            				cjmData[i] = chartDay.totalSuccessNum;
            				jemData[i] = chartDay.totalPrice;
            				head+="<th>"+chartDay.day+"</th>";
            				tr1+="<td>"+chartDay.totalCancelNum+"</td>";
            				tr2+="<td>"+chartDay.totalSuccessNum+"</td>";
            				tr3+="<td>"+chartDay.totalPrice+"</td>";
            			}
            			head=head+"</tr>"
            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
            			$("#daijiaHead tr").remove();
            			$("#daijiaHead").append(head);
            			$("#daijiaTable tbody").remove();
            			$("#daijiaTable").append(newcontent);
            			$("#daijiaTable tbody").show(300);
            			renderMonthChart(year,month,day,xdmData,cjmData,jemData);
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '代驾订单统计图'
        },
        subtitle: {
            text: year+'年'
        },
        xAxis: [{
            categories: Highcharts.getOptions().lang.months
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};


renderMonthChart=function(year,month,days,xdData,cjData,jeData){
	
	$("#chart").highcharts({
		chart: {
            renderTo:  'chart',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		var category = e.point.category;
	            		//加载月数据 
	            		djday=category;
	            		$.post(window.daijia.ctx+'/chart/dataForDay',{'year':year,'month':month,'day':category,'companyId':$("#companyId").val()},function(data){
	            			
	            			var dataHours = data.hours;
	            			var hours = new Array();
	            			
	            			var xddData = new Array();
	            			var cjdData = new Array();
	            			var jedData = new Array();
	            			var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\小时</th>";
	            			var tr1='<td>销单量</td>';
	            			var tr2='<td>完成单量</td>';
	            			var tr3='<td>金额(元)</td>';
	            			for(var i=0 ; i<dataHours.length ; i++){
	            				var hour = dataHours[i];
	            				hours[i] = hour.hour;
	            				xddData[i] = hour.totalCancel;
	            				cjdData[i] = hour.totalSuccess;
	            				jedData[i] = hour.totalPrice;
	            				head+="<th>"+hour.hour+"</th>";
	            				tr1+="<td>"+hour.totalCancel+"</td>";
	            				tr2+="<td>"+hour.totalSuccess+"</td>";
	            				tr3+="<td>"+hour.totalPrice+"</td>";
	            			}
	            			head=head+"</tr>"
	            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
	            			$("#daijiaHead tr").remove();
	            			$("#daijiaHead").append(head);
	            			$("#daijiaTable tbody").remove();
	            			$("#daijiaTable").append(newcontent);
	            			$("#daijiaTable tbody").show(300);
	            			
	            			renderDayChart(year,month,category,hours,xddData,cjdData,jedData);
	            			
	            		},'json');
	            		
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '代驾订单统计图'
        },
        subtitle: {
            text: year+"年-"+(month+1)+"月"
        },
        xAxis: [{
            categories: days
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jeData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xdData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};

renderDayChart=function(year,month,day,hours,xddData,cjdData,jedData){
	
	$("#chart").highcharts({
		chart: {
            renderTo: 'chart',
            zoomType: 'xy'
        },
		plotOptions: {
			series:{
				cursor: 'pointer',
	            events:{
	            	click:function(e){
	            		//TODO 返回年数据统计
	            		djmonth="";
	            		djday="";
	            		djhour="";
	            		var head = "<tr style='font-size:14px;'><th style='width:90px;'>分类\\月份</th>";
            			var tr1='<td>销单量</td>';
            			var tr2='<td>完成单量</td>';
            			var tr3='<td>金额(元)</td>';
            			for(var i=0;i<months.length;i++){
	            			head+="<th>"+((months[i].month)+1)+"</th>";
	            			tr1+="<td>"+(months[i]).totalCancelNum+"</td>";
            			    tr2+="<td>"+(months[i]).totalSuccessNum+"</td>";
            			    tr3+="<td>"+(months[i]).totalPrice+"</td>";
	            		}
	            		head=head+"</tr>"
            			var newcontent = "<tr>"+tr1+"</tr><tr>"+tr2+"</tr><tr>"+tr3+"</tr>";
            			$("#daijiaHead tr").remove();
            			$("#daijiaHead").append(head);
            			$("#daijiaTable tbody").remove();
            			$("#daijiaTable").append(newcontent);
            			$("#daijiaTable tbody").show(300);
	            		renderYearChart(xdData,cjData,jeData,months);
	            	}
	            }
			}
        },
        credits:{
        	enabled:false
        },
        title: {
            text: '代驾订单统计图'
        },
        subtitle: {
            text: year+"年-"+(month+1)+"月-"+day+"日"
        },
        xAxis: [{
            categories: hours
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}单',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: '单量',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            allowDecimals: false
        }, { // Secondary yAxis
            title: {
                text: '总价',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} 元',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: '金额',
            type: 'column',
            yAxis: 1,
            data: jedData,
            tooltip: {
                valueSuffix: '元'
            }

        }, {
            name: '单量',
            type: 'spline',
            data: cjdData,
            tooltip: {
                valueSuffix: '单'
            }
        }, {
            name: '已销单',
            type: 'spline',
            data: xddData,
            tooltip: {
                valueSuffix: '个'
            }
        }]
    });
	
};